package com.zq.assistant.func.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.zq.assistant.app.App;
import com.zq.assistant.func.db.table.DownloadInfo;
import com.zq.assistant.func.db.table.User;
import com.zq.db.bean.DefaultSQLBeanCreator;
import com.zq.db.bean.SQLBean;
import com.zq.db.dao.Dao;
import com.zq.db.dao.factory.DaoFactory;
import com.zq.db.sql.factory.SQLFactory;

/**
 * Created by zhangqiang on 17-6-8.
 */

public class DBHelper extends SQLiteOpenHelper{

    private static final String DB_NAME = "database";
    private static final int DB_VERSION = 15;

    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public static <T extends SQLBean> Dao<T> getDao(Class<T> tableClass){

        return SQLFactory.IMPL.getDao(SQLFactory.IMPL.getAndroidSQL(new DBHelper(App.instance)),tableClass);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(SQLFactory.IMPL.getTableHandler(SQLFactory.IMPL.getAndroidSQL(this),User.class).getTableCreateSQL());
        db.execSQL(SQLFactory.IMPL.getTableHandler(SQLFactory.IMPL.getAndroidSQL(this), com.zq.download.bean.DownloadInfo.class).getTableCreateSQL());
        db.execSQL(SQLFactory.IMPL.getTableHandler(SQLFactory.IMPL.getAndroidSQL(this), DownloadInfo.class).getTableCreateSQL());
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL(SQLFactory.IMPL.getTableHandler(SQLFactory.IMPL.getAndroidSQL(this),User.class).getTableDropSQL());
        db.execSQL(SQLFactory.IMPL.getTableHandler(SQLFactory.IMPL.getAndroidSQL(this), com.zq.download.bean.DownloadInfo.class).getTableDropSQL());

        db.execSQL(SQLFactory.IMPL.getTableHandler(SQLFactory.IMPL.getAndroidSQL(this), DownloadInfo.class).getTableDropSQL());
        onCreate(db);
    }

}
